<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp  "&#160;">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:encoder="xalan://java.net.URLEncoder"
xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"
xmlns:dsm="http://www.educationau.edu.au/"
xmlns:atom="http://www.w3.org/2005/Atom"
exclude-result-prefixes="xsl encoder">

<xsl:output indent="no"
          method="xml"
        omit-xml-declaration="no"/>
        
<xsl:variable name="found" select="/results/summary/found"/>
<xsl:variable name="query" select="/results/summary/qp[@name='q']"/>
        
<xsl:template match="/">
	
		 <rss version="2.0">
      <channel>
        <language>en</language>
        <copyright><xsl:value-of select="/results/copyright"/></copyright>
        <managingEditor>2</managingEditor>
        <link>http://dsm.educationau.edu.au</link>
        <title>DSM Search for '<xsl:value-of select="/results/summary/qp[@name='q']"/>'</title>
        <description><xsl:value-of select="/results/summary/source"/> </description>
        <opensearch:totalResults><xsl:value-of select="/results/summary/found"/></opensearch:totalResults>
        <opensearch:startIndex><xsl:value-of select="/results/summary/qp[@name='start']"/></opensearch:startIndex>
        <opensearch:itemsPerPage><xsl:value-of select="/results/summary/qp[@name='ps']"/></opensearch:itemsPerPage>
        <atom:link rel="search" type="application/opensearchdescription+xml" href="http://example.com/opensearchdescription.xml"/>
        <opensearch:Query role="request" searchTerms="{$query}" startPage="1" />
        
        <dsm:mode><xsl:value-of select="/results/summary/mode"/></dsm:mode>
        <dsm:token><xsl:value-of select="/results/summary/token"/></dsm:token>
        <dsm:completed><xsl:value-of select="/results/summary/completed"/></dsm:completed>

        <xsl:apply-templates select="/results/sources"/>
        
        <xsl:if test="$found&gt;0">
          <xsl:apply-templates select="/results/item"/>
        </xsl:if>
      </channel>
    </rss>
  </xsl:template>

  <xsl:template match="/results/sources|/results/sources//*">
     <xsl:element name="{concat('dsm:',local-name())}">
       <xsl:copy-of select="@*"/>
       <xsl:apply-templates/>
     </xsl:element>
  </xsl:template>

  <xsl:template match="/results/item">
    <xsl:variable name="sourceid" select="@source" />
   
       <item>

          <title><xsl:value-of select="normalize-space(./title)"/>
           <xsl:value-of select="/results/sources/source[@id=$sourceid]/title"/>
          </title>
          <link><xsl:value-of select="./link"/></link>
          <description><xsl:value-of select="./description"/></description>
         
         <xsl:if test="string-length(./resultsource) &gt; 0"><dsm:source><xsl:value-of select="./resultsource"/></dsm:source> </xsl:if>
       	 <xsl:if test="string-length(./relevance) &gt; 0"><dsm:relevance><xsl:value-of select="./relevance"/></dsm:relevance></xsl:if>
      
		 <xsl:if test="string-length(./date) &gt; 0"><dsm:date><xsl:value-of select="./date"/></dsm:date></xsl:if>
		 <xsl:if test="string-length(./download) &gt; 0"><dsm:download><xsl:value-of select="./download"/></dsm:download></xsl:if>
       	 <xsl:if test="string-length(./preview) &gt; 0"><dsm:preview><xsl:value-of select="./preview"/></dsm:preview></xsl:if>
       	 <xsl:if test="string-length(./identifier) &gt; 0"><dsm:identifier><xsl:value-of select="./identifier"/></dsm:identifier></xsl:if>
         <xsl:if test="string-length(./rights) &gt; 0"><dsm:rights><xsl:value-of select="./rights"/></dsm:rights></xsl:if>
         <xsl:if test="string-length(./metadatascheme) &gt; 0"><dsm:metadatascheme><xsl:value-of select="./metadatascheme"/></dsm:metadatascheme></xsl:if>
         <xsl:if test="string-length(./contentpackage) &gt; 0"><dsm:contentpackage><xsl:value-of select="./contentpackage"/></dsm:contentpackage></xsl:if>

         <xsl:call-template name="processMetadataItems"/>

       </item>
 
  </xsl:template>

  <xsl:template name="processMetadataItems">
     <xsl:apply-templates select="./metadata"/>
  </xsl:template>
  
  <xsl:template match="/results/item/metadata">
    <xsl:if test="string-length(./) &gt; 0"><dsm:metadata name="{self::node()/@name}"> <xsl:value-of select="./"/></dsm:metadata></xsl:if>
  </xsl:template>
	
</xsl:stylesheet>